package com.xuchen.zhenyang.controller.statistics;


import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.pagination.PageHelper;
import com.xuchen.zhenyang.base.BaseQuery;
import com.xuchen.zhenyang.base.Result;
import com.xuchen.zhenyang.core.annotation.RequestLog;
import com.xuchen.zhenyang.entity.Goods;
import com.xuchen.zhenyang.entity.base.MyEntityWrapper;
import com.xuchen.zhenyang.entity.statistics.StatisticsGoods;
import com.xuchen.zhenyang.entity.statistics.StatisticsGoodsMonth;
import com.xuchen.zhenyang.entity.statistics.StatisticsGoodsYear;
import com.xuchen.zhenyang.model.statistics.StatisticsGoodsQuery;
import com.xuchen.zhenyang.model.statistics.StatisticsGoodsSaleModel;
import com.xuchen.zhenyang.service.GoodsService;
import com.xuchen.zhenyang.service.statistics.StatisticsGoodsMonthService;
import com.xuchen.zhenyang.service.statistics.StatisticsGoodsService;
import com.xuchen.zhenyang.service.statistics.StatisticsGoodsYearService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 控制器
 * </p>
 *
 * @author xuchen
 * @since 2019-05-06
 */
@Controller
@RequestMapping("/statisticsGoods")
@RequiresPermissions("statisticsGoods")
public class StatisticsGoodsController {

    @Autowired
    StatisticsGoodsService statisticsGoodsService;
    @Autowired
    StatisticsGoodsMonthService statisticsGoodsMonthService;
    @Autowired
    StatisticsGoodsYearService statisticsGoodsYearService;
    @Autowired
    GoodsService goodsService;

    @GetMapping("")
    String index(HttpServletRequest request) {
        setAttributeEnums(request);
        return "statistics/goods/statistics-goods-list";
    }

    @GetMapping("list")
    @ResponseBody
    @RequestLog
    Result list(BaseQuery baseQuery, StatisticsGoodsQuery goodsQuery) {
        List<StatisticsGoodsSaleModel> list = new ArrayList<>(baseQuery.getLimit());
        if (goodsQuery == null) {
            goodsQuery = new StatisticsGoodsQuery();
        }
        if (goodsQuery.getTimeType() == null) {
            //默认查询月
            goodsQuery.setTimeType(1);
        }
        switch (goodsQuery.getTimeType()) {
            case 1:
                StatisticsGoodsMonth goodsMonth = new StatisticsGoodsMonth();
                goodsMonth.setGoodsId(goodsQuery.getGoodsId());
                MyEntityWrapper<StatisticsGoodsMonth> wrapper = new MyEntityWrapper<>(baseQuery, goodsMonth);
                wrapper.eq("goods_id").le("month_time", baseQuery.getMonthEndDate()).ge("month_time", baseQuery.getMonthBeginDate());
                statisticsGoodsMonthService.selectList(wrapper).forEach(e -> {
                    StatisticsGoodsSaleModel saleModel = new StatisticsGoodsSaleModel();
                    BeanUtils.copyProperties(e, saleModel);
                    saleModel.setSaleTime(String.format("%d年%d月", e.getMonthTime() / 100, e.getMonthTime() % 100));
                    list.add(saleModel);
                });
                return Result.success(PageHelper.freeTotal(), list);
            case 2:
                StatisticsGoodsYear goodsYear = new StatisticsGoodsYear();
                goodsYear.setGoodsId(goodsQuery.getGoodsId());
                MyEntityWrapper<StatisticsGoodsYear> yearWrapper = new MyEntityWrapper<>(baseQuery, goodsYear);
                yearWrapper.eq("goods_id").le("year_time", baseQuery.getYearEndDate()).ge("year_time", baseQuery.getYearBeginDate());
                statisticsGoodsYearService.selectList(yearWrapper).forEach(e -> {
                    StatisticsGoodsSaleModel saleModel = new StatisticsGoodsSaleModel();
                    BeanUtils.copyProperties(e, saleModel);
                    saleModel.setSaleTime(String.format("%d年", e.getYearTime()));
                    list.add(saleModel);
                });
                return Result.success(PageHelper.freeTotal(), list);
            case 3:
                StatisticsGoods goods = new StatisticsGoods();
                goods.setGoodsId(goodsQuery.getGoodsId());
                MyEntityWrapper<StatisticsGoods> goodsWrapper = new MyEntityWrapper<>(baseQuery, goods);
                goodsWrapper.eq("goods_id");
                statisticsGoodsService.selectList(goodsWrapper).forEach(e -> {
                    StatisticsGoodsSaleModel saleModel = new StatisticsGoodsSaleModel();
                    BeanUtils.copyProperties(e, saleModel);
                    saleModel.setSaleTime("所有时间");
                    list.add(saleModel);
                });
                return Result.success(PageHelper.freeTotal(), list);
            default:
                return null;
        }
    }

    private void setAttributeEnums(HttpServletRequest request) {
        List<Goods> list = goodsService.selectList(new EntityWrapper<>());
        request.setAttribute("goodsList", list);
    }
}

